<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;

class Cainiankaoheqi extends Common
{

//    财年列表
    public function index()
    {
        $count = Db::table('cg_cainian')->count();
        $data = Db::table('cg_cainian')->order("id desc")->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
            $data[$k]['starttime'] = date('Y-m-d',$v['starttime']);
            $data[$k]['endtime'] = date('Y-m-d',$v['endtime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
		$this->assign('maxpage',$maxpage);
		$this->assign('count',$count);
        return $this->fetch('index');
    }

//    添加财年
    public function add(){

        $validate = new Validate([
            'title|财年名称'  => 'require|max:100|unique:cg_cainian',
            'starttime|开始日期' => 'require|max:200|lt:endtime|dateFormat:Y-m-d',
            'endtime|结束日期' => 'require|dateFormat:Y-m-d',
        ]);

        $data = [];
        $data['title'] = $this->request->post('title');
        $data['starttime'] = $this->request->post('starttime');
        $data['endtime'] = $this->request->post('endtime');
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }



        $data['starttime'] = strtotime($data['starttime']);
        $data['endtime'] = strtotime($data['endtime']);
        $data['cuser'] = $_SESSION['think']['employee_id'];
        $data['ctime'] = time();
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();

        if($data['endtime']<= $data['starttime']){
             echo  json_encode([
                "success"=>false,
                "msg"=>"开始时间必须在结束时间之前"
            ]);die;
        }

        $chongtu=Db::table("cg_cainian")->where("(starttime<={$data['endtime']} and endtime>={$data['starttime']})")->count();
       
        if($chongtu){
             echo  json_encode([
                "success"=>false,
                "msg"=>"财年时间与已存在的财年存在冲突"
            ]);die;
        }

        $bool = Db::table('cg_cainian')->insert($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"添加成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"添加失败"
            ]);die;
        }

    }

//    修改财年
    public function update(){

        $id = $this->request->post('id');
        $validate = new Validate([
            'title'  => 'require|max:100',
            'starttime' => 'require|max:200',
            'endtime' => 'require',
            'id' => 'require',
        ]);
        $data = [];
        $data['title'] = $this->request->post('title');
        $data['starttime'] = $this->request->post('starttime');
        $data['endtime'] = $this->request->post('endtime');
        $data['id'] = $id;
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }
        $data['starttime'] = strtotime($data['starttime']);
        $data['endtime'] = strtotime($data['endtime']);
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();

         if($data['endtime']<= $data['starttime']){
             echo  json_encode([
                "success"=>false,
                "msg"=>"开始时间必须在结束时间之前"
            ]);die;
        }

          $chongtu=Db::table("cg_cainian")->where("(starttime<={$data['endtime']} and endtime>={$data['starttime']}) and id <>{$id}")->count();
        if($chongtu){
             echo  json_encode([
                "success"=>false,
                "msg"=>"财年时间与已存在的其他财年存在冲突"
            ]);die;
        }


        $bool = Db::table('cg_cainian')->where(array('id'=>$id))->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"修改成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"修改失败"
            ]);die;
        }

    }

//    删除财年
    public function delete(){
        $id = $this->request->post('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_cainian')->where(array('id'=>$id))->delete();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }

//    一级考核期
    public function kaoheqi1(){
        $cid = $this->request->param('id');
        $this->assign('cid',$cid);

        $cainian_title = Db::table('cg_cainian')->where(array('id'=>$cid))->value('title');
        $this->assign('cainian_title',$cainian_title);

        $count = Db::table('cg_kaoheqi1')->where(array('cid'=>$cid))->count();
        $data = Db::table('cg_kaoheqi1')->where(array('cid'=>$cid))->order("id desc")->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
            $data[$k]['starttime'] = date('Y-m-d',$v['starttime']);
            $data[$k]['endtime'] = date('Y-m-d',$v['endtime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);

        return $this->fetch('kaoheqi1');
    }
//    增加一级考核期
    public function kaoheqi1_add(){

        $data = [];
        $data['sn'] = $this->request->post('sn');
        $data['cid'] = $this->request->post('cid');
        $data['title'] = $this->request->post('title');
        $data['starttime'] = $this->request->post('starttime');
        $data['endtime'] = $this->request->post('endtime');

        $validate = new Validate([
            'sn|考核期序号'  => 'require|number',
            'title|考核期名称'  => 'require|max:100',
            'starttime|开始日期' => 'require|max:200|lt:endtime|dateFormat:Y-m-d',
            'endtime|结束日期' => 'require|dateFormat:Y-m-d',
            'cid|对应财年' => 'require',
        ]);

        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        //验证sn是否重复
        $sncnt = Db::table('cg_kaoheqi1')->where(array('cid'=>$data['cid'],'sn'=>$data['sn']))->count();
        if($sncnt){
            echo  json_encode([
                "success"=>false,
                "msg"=>'考核期序号已存在'
            ]);die;
        }

        $data['starttime'] = strtotime($data['starttime']);
        $data['endtime'] = strtotime($data['endtime']);
        $data['cuser'] = $_SESSION['think']['employee_id'];
        $data['ctime'] = time();
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();

         if($data['endtime']<= $data['starttime']){
             echo  json_encode([
                "success"=>false,
                "msg"=>"开始时间必须在结束时间之前"
            ]);die;
        }

        //检验考核期时间是否在财年之中
        $cainian=Db::table("cg_cainian")->where(["id"=>$data["cid"]])->find();

        if(($data['starttime']<$cainian["starttime"])||($data['endtime']>$cainian["endtime"])){
              echo  json_encode([
                "success"=>false,
                "msg"=>"考核期起止时间超出上级财年的范围"
            ]);die;
        }

         $chongtu=Db::table("cg_kaoheqi1")->where("(starttime<={$data['endtime']} and endtime>={$data['starttime']}) and cid={$data["cid"]}")->count();
        if($chongtu){
             echo  json_encode([
                "success"=>false,
                "msg"=>"考核期时间与已存在的其他一级考核期时间存在冲突"
            ]);die;
        }



        $bool = Db::table('cg_kaoheqi1')->insert($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"添加成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"添加失败"
            ]);die;
        }

    }
    //    修改一级考核期
    public function kaoheqi1_update(){

        $id = $this->request->post('id');
        $cid = $this->request->post('cid');
        $validate = new Validate([
            'title'  => 'require|max:100',
            'starttime' => 'require|max:200',
            'endtime' => 'require',
            'id' => 'require',
            'cid' => 'require',
        ]);
        $data = [];
        $data['title'] = $this->request->post('title');
        $data['starttime'] = $this->request->post('starttime');
        $data['endtime'] = $this->request->post('endtime');
        $data['id'] = $id;
        $data['cid'] = $cid;
        $data['sn'] = $this->request->post('sn');
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        //验证sn是否重复
        $sncnt = Db::table('cg_kaoheqi1')
            ->where(array(
                'cid'=>$data['cid'],
                'sn'=>$data['sn'],
                'id'=>['<>',$id]
            ))
            ->count();
        if($sncnt){
            echo  json_encode([
                "success"=>false,
                "msg"=>'考核期序号已存在'
            ]);die;
        }

        $data['starttime'] = strtotime($data['starttime']);
        $data['endtime'] = strtotime($data['endtime']);
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();



         if($data['endtime']<= $data['starttime']){
             echo  json_encode([
                "success"=>false,
                "msg"=>"开始时间必须在结束时间之前"
            ]);die;
        }

        //检验考核期时间是否在财年之中
         $cainian=Db::table("cg_cainian")->where(["id"=>$data["cid"]])->find();

        if(($data['starttime']<$cainian["starttime"])||($data['endtime']>$cainian["endtime"])){
              echo  json_encode([
                "success"=>false,
                "msg"=>"考核期起止时间超出上级财年的范围"
            ]);die;
        }

         $chongtu=Db::table("cg_kaoheqi1")->where("(starttime<={$data['endtime']} and endtime>={$data['starttime']}) and id<>{$id} and cid={$data["cid"]}")->count();
        if($chongtu){
             echo  json_encode([
                "success"=>false,
                "msg"=>"考核期时间与已存在的其他一级考核期时间存在冲突"
            ]);die;
        }

        $bool = Db::table('cg_kaoheqi1')->where(array('id'=>$id))->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"修改成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"修改失败"
            ]);die;
        }

    }

//    考核期一删除
    public function kaoheqi1_delete(){
        $id = $this->request->post('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_kaoheqi1')->where(array('id'=>$id))->delete();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }

    //    二级考核期
    public function kaoheqi2(){
        $pid = $this->request->param('id');
        $cid = $this->request->param('cid');
        $this->assign('pid',$pid);
        $this->assign('cid',$cid);

        //查询一级考核期
        $kaoheqi1_title = Db::table('cg_kaoheqi1')->where(array('id'=>$pid))->value('title');
        $cid = Db::table('cg_kaoheqi1')->where(array('id'=>$pid))->value('cid');
        $cainian_title = Db::table('cg_cainian')->where(array('id'=>$cid))->value('title');
        $this->assign('kaoheqi1_title',$kaoheqi1_title);
        $this->assign('cainian_title',$cainian_title);

        $count = Db::table('cg_kaoheqi2')->where(array('pid'=>$pid))->count();
        $data = Db::table('cg_kaoheqi2')->where(array('pid'=>$pid))->order("id desc")->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
            $data[$k]['starttime'] = date('Y-m-d',$v['starttime']);
            $data[$k]['endtime'] = date('Y-m-d',$v['endtime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);

        return $this->fetch();
    }

    //    增加二级考核期
    public function kaoheqi2_add(){

        $validate = new Validate([
            'sn'  => 'require|number',
            'title'  => 'require|max:100',
            'starttime' => 'require|max:200|lt:endtime',
            'endtime' => 'require',
            'pid' => 'require',
        ]);

        $data = [];
        $data['sn'] = $this->request->post('sn');
        $data['pid'] = $this->request->post('pid');
        $data['title'] = $this->request->post('title');
        $data['starttime'] = $this->request->post('starttime');
        $data['endtime'] = $this->request->post('endtime');
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }

        $sncnt = Db::table('cg_kaoheqi2')->where(array('pid'=>$data['pid'],'sn'=>$data['sn']))->count();
        if($sncnt){
            echo  json_encode([
                "success"=>false,
                "msg"=>'考核期序号已存在'
            ]);die;
        }

        $data['starttime'] = strtotime($data['starttime']);
        $data['endtime'] = strtotime($data['endtime']);
        $data['cuser'] = $_SESSION['think']['employee_id'];
        $data['ctime'] = time();
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();

           if($data['endtime']<= $data['starttime']){
             echo  json_encode([
                "success"=>false,
                "msg"=>"开始时间必须在结束时间之前"
            ]);die;
        }

        //检验考核期时间是否在上级之中
        $cainian=Db::table("cg_kaoheqi1")->where(["id"=>$data["pid"]])->find();

        if(($data['starttime']<$cainian["starttime"])||($data['endtime']>$cainian["endtime"])){
              echo  json_encode([
                "success"=>false,
                "msg"=>"考核期起止时间超出上级一级考核期的范围"
            ]);die;
        }

         $chongtu=Db::table("cg_kaoheqi2")->where("(starttime<={$data['endtime']} and endtime>={$data['starttime']}) and pid={$data["pid"]}")->count();
        if($chongtu){
             echo  json_encode([
                "success"=>false,
                "msg"=>"考核期时间与已存在的其他二级考核期时间存在冲突"
            ]);die;
        }





        $bool = Db::table('cg_kaoheqi2')->insert($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"添加成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"添加失败"
            ]);die;
        }

    }

    //    修改二级考核期
    public function kaoheqi2_update(){

        $id = $this->request->post('id');
        $pid = $this->request->post('pid');
        $validate = new Validate([
            'title'  => 'require|max:100',
            'starttime' => 'require|max:200',
            'endtime' => 'require',
            'id' => 'require',
            'pid' => 'require',
            'sn' => 'require',
        ]);
        $data = [];
        $data['title'] = $this->request->post('title');
        $data['starttime'] = $this->request->post('starttime');
        $data['endtime'] = $this->request->post('endtime');
        $data['sn'] = $this->request->post('sn');
        $data['id'] = $id;
        $data['pid'] = $pid;
        if (!$validate->check($data)) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$validate->getError()
            ]);die;
        }
        $data['starttime'] = strtotime($data['starttime']);
        $data['endtime'] = strtotime($data['endtime']);
        $data['puser'] = $_SESSION['think']['employee_id'];
        $data['ptime'] = time();

            if($data['endtime']<= $data['starttime']){
             echo  json_encode([
                "success"=>false,
                "msg"=>"开始时间必须在结束时间之前"
            ]);die;
        }

        //检验考核期时间是否在上级之中
        $cainian=Db::table("cg_kaoheqi1")->where(["id"=>$data["pid"]])->find();

        if(($data['starttime']<$cainian["starttime"])||($data['endtime']>$cainian["endtime"])){
              echo  json_encode([
                "success"=>false,
                "msg"=>"考核期起止时间超出上级一级考核期的范围"
            ]);die;
        }

         $chongtu=Db::table("cg_kaoheqi2")->where("(starttime<={$data['endtime']} and endtime>={$data['starttime']}) and pid={$data["pid"]} and id<>$id")->count();
        if($chongtu){
             echo  json_encode([
                "success"=>false,
                "msg"=>"考核期时间与已存在的其他二级考核期时间存在冲突"
            ]);die;
        }


        $bool = Db::table('cg_kaoheqi2')->where(array('id'=>$id))->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"修改成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"修改失败"
            ]);die;
        }

    }

    //    考核期二删除
    public function kaoheqi2_delete(){
        $id = $this->request->post('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_kaoheqi2')->where(array('id'=>$id))->delete();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }
}
